package com.corpdata.app.finance.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.corpdata.core.base.AbstractBaseService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.corpdata.app.finance.dao.FinancialSettingMapper;
import com.corpdata.app.finance.entity.FinancialSetting;
import com.corpdata.app.finance.service.FinancialSettingService;
import com.corpdata.common.result.Result;
import com.corpdata.common.utils.CorpdataUtil;

/**
 * 
 * 
 * @author wu
 * @email 64088839@qq.com
 * @date 2018-07-27 16:44:31
 */
@Service
@Transactional
public class FinancialSettingServiceImpl extends AbstractBaseService<FinancialSetting> implements FinancialSettingService {
	@Autowired
	private FinancialSettingMapper setMapper;
	
	//保存财务设置
	@Override
	public Result saveSetting(JSONArray list) {
		Result r = null;
		for(int i = 0;i<list.size();i++){
			JSONObject obj = list.getJSONObject(i);
			//查询该类型设置是否存在，如果存在编辑，否则新增
			FinancialSetting record = setMapper.findByStatus(obj.getString("status"));
			if(record == null){
				record = new FinancialSetting();
				record.setId(CorpdataUtil.getUUID());
				record.setStatus(obj.getString("status"));
				record.setDuration(obj.getInteger("duration"));
				if(!StringUtils.isBlank(obj.getString("type"))){
					record.setType(obj.getString("type"));
				}
				r = super.save(record);
				
			}else{
				record.setDuration(obj.getInteger("duration"));
				if(!StringUtils.isBlank(obj.getString("type"))){
					record.setType(obj.getString("type"));
				}
				r = super.update(record);
			}
		}
		return r;
	}

	@Override
	public List<FinancialSetting> findAll() {
		// TODO Auto-generated method stub
		return setMapper.selectAll(null);
	}

	@Override
	public FinancialSetting findByStatus(String status) {
		// TODO Auto-generated method stub
		return setMapper.findByStatus(status);
	}
	
}
